﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Diagnostics;
using System.Windows.Forms;
using System.Globalization;
using System.IO;
using System.Xml;
using System.Windows.Forms.Design;
using System.Runtime.Serialization;

namespace VoltcraftControlInterface
{
    public partial class MainForm : Form
    {
        public bool shouldlogpower;
        public delegate void SetPDel(double v2, double v1, double c2, double c1, double t2, double t1, double elapsed, double now);
        public void SetPower(double v2, double v1, double c2, double c1, double t2, double t1, double elapsed, double now)
        {
            if (this.powLbl.InvokeRequired)
            {
                SetPDel d = new SetPDel(SetPower);
                Invoke(d, new object[] { v2, v1, c2, c1, t2, t1, elapsed, now });
            }
            else
            {
                powLbl.Text = (v2 * c2).ToString("F2");
                double prev = Double.Parse(enerLbl.Text);
                prev += ((((v2 * c2) + (v1 * c1)) / 2) * (t2 - t1));
                enerLbl.Text = prev.ToString("N3", new CultureInfo("it-IT"));
                if(shouldlogpower)
                {
                    powChart.Series[0].Points.AddXY(elapsed, Double.Parse(enerLbl.Text), now);
                    powChart.Series[1].Points.AddXY(elapsed, (v2 * c2));
                }
            }
        }

        private void exportPowBtn_Click(object sender, EventArgs e)
        {
            SaveFileDialog filepath = new SaveFileDialog();
            filepath.Filter = "CSV Spreadsheet|*.csv";
            filepath.Title = "Save Logged Data";
            filepath.ShowDialog();
            if (filepath.FileName != "")
            {
                if (System.IO.File.Exists(filepath.FileName)) System.IO.File.Delete(filepath.FileName);
                using (System.IO.StreamWriter file =
              new System.IO.StreamWriter(@filepath.FileName, true))
                {
                    string line = "Time(s);Time;Energy Consumed (J);Power Consumption (W)";
                    file.WriteLine(line);
                    for (int i = 0; i < powChart.Series[0].Points.Count; i++)
                    {
                        //parsetime = DateTime.FromOADate(logChart.Series["Voltage"].Points[i].XValue);
                        file.WriteLine(powChart.Series[0].Points[i].XValue + ";" + DateTime.FromOADate(powChart.Series[0].Points[i].YValues[1]).ToLongTimeString() + "." + DateTime.FromOADate(powChart.Series[0].Points[i].YValues[1]).Millisecond + ";" + powChart.Series[0].Points[i].YValues[0].ToString("F3") + ";" + powChart.Series[1].Points[i].YValues[0].ToString("F2"));
                    }
                    file.Close();

                }
            }
        }
    }
}
